package com.x.provider.oss.component;

import com.x.provider.oss.service.ContentGreenService;
import com.x.provider.oss.service.RedisKeyService;
import com.x.redis.service.DistributeRedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@EnableScheduling
public class JobComponent {

    private final RedisKeyService redisKeyService;
    private final ContentGreenService contentReviewService;

    public JobComponent(RedisKeyService redisKeyService,
                        ContentGreenService contentReviewService){
        this.redisKeyService = redisKeyService;
        this.contentReviewService = contentReviewService;
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 6000)
    private void updateContentReviewResult() {
        try(DistributeRedisLock lock = new DistributeRedisLock(redisKeyService.getUpdateContentReviewResultLockKey())){
            if (!lock.tryLock()){
                return;
            }
            contentReviewService.updateContentGreenResult();
        }
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 6000)
    private void notifyContentReviewResult() {
        try(DistributeRedisLock lock = new DistributeRedisLock(redisKeyService.getContentReviewNotifyLockKey())){
            if (!lock.tryLock()){
                return;
            }
            contentReviewService.notifyContentGreenResult();
        }
    }
}
